<?php
require_once(APPPATH."models/base.php"); 

class User_Get_Type
{
	const ID = 1;
	const Name = 2;
	const Multi = 3; 
}

class user_model extends base_model
{ 
	function __construct()
	{
		parent::__construct();
	}
	
	function get_group()
	{
		$usergroup = $this->usergroup_manager->get($this->usergroup_id);
		return $usergroup;
	
	}
}

class user_manager extends base_manager
{
	public function get($param,$type = User_Get_Type::ID)
	{
		if($type == User_Get_Type::ID)
		{
			return $this->db->get_where('user',array('id'=>$param))->row();
		}
		else if($type == User_Get_Type::Name) 
		{
			return $this->db->get_where('user',array('name'=>$param))->row();
		}
		else
		{
			foreach($param as $key=>$value)
			{
				// echo $key;
				$this->db->where($key,$value);
			}
			return $this->db->get('user')->row();  
		}
	}
	public function get_list($where = Array())
	{
		$usergroup_id = array_remove($where, 'group.id');
		$descendant = array_remove($where, 'descendant');
		//echo $usergroup_alias;exit;
		$usergroup = null;
		if($usergroup_id != null)
		{
			$usergroup = $this->usergroup_manager->get($usergroup_id);
		}
		if($usergroup!=null)  // 类ID不为空的情况
		{
			$descendant == null && $descendant = 'false';
		
			if($descendant == "false")
			{
				$where['group.id'] = $usergroup->id;
			}
			else
			{
				$ids = Array($usergroup->id);
				// print_r($ids);
				$descendant = $usergroup->descendant;
				if(!function_exists('usergroup_cube'))
				{
					function usergroup_cube($v)
					{
						return $v->id;
					}
				}
				$ids = array_merge($ids,array_map('usergroup_cube',$descendant));
				// print_r($ids);exit;
				$ids = trim(implode(",", $ids),',');
				$where['group.id'] = "in(".$ids.")";
			}
		}
		$query_result = new stdClass();
		$page = array_remove($where,'page');$page == null && $page = 1 ;
		$page_size = array_remove($where,'page_size');
		
		$order_by = array_remove($where,'order_by');
		
		$t_user = _T("user");$t_usergroup = _T("usergroup");
		
		
		$where_string = "where 1=1 ";
		
		$where_string .= $this->build_where_string($where);
		
		$sql_pattern =
		@"select %s from $t_user user
		left join $t_usergroup usergroup on user.usergroup_id=usergroup.id %s";
		
		$count_sql = sprintf($sql_pattern,"count(*) as row_number",$where_string);
		// echo $count_sql;exit ;
		
		$query_result->record_count = $this->db->query($count_sql)->row()->row_number;
		
		if($order_by != null)
		{
			$sql_pattern .= " order by $order_by";
		}
		else
		{
			$sql_pattern .= " order by id desc";
		}
		// var_dump($page_size);exit;
		if($page_size != null)
		{
			$begin_index = ($page-1)*$page_size;
			$query_result->page_count = intval($query_result->record_count/$page_size)-1;
			$sql_pattern .= " limit $begin_index,$page_size";
		}
		
		$result_sql = sprintf(
				$sql_pattern,
				@"user.*",
				$where_string
		);
		
		//echo $result_sql;exit;
		$this->query_result = $query_result;
		return $this->convert_list(
				$this->db->query($result_sql)->result()
		);
	}
} 

?> 
